package com.atguigu.chapter11;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.DataTypes;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.descriptors.Csv;
import org.apache.flink.table.descriptors.FileSystem;
import org.apache.flink.table.descriptors.Schema;

import static org.apache.flink.table.api.Expressions.$;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/7/24 10:03
 */
public class Flink03_Connector_File {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        
        StreamTableEnvironment tenv = StreamTableEnvironment.create(env);
        
        Schema schema = new Schema()
            .field("id", DataTypes.STRING())
            .field("ts", DataTypes.BIGINT())
            .field("vc", DataTypes.INT());
        tenv
            .connect(new FileSystem().path("input/sensor.txt"))
            .withFormat(new Csv())
            .withSchema(schema) // 指定表中字段和字段的类型
            .createTemporaryTable("sensor");
    
        Table t1 = tenv.from("sensor");
    
        Table result = t1
            .select($("id"), $("vc"));
    
    
        Schema schema1 = new Schema()
            .field("id", DataTypes.STRING())
            .field("vc", DataTypes.INT());
        
        // 关联文件的sink
        tenv
            .connect(new FileSystem().path("input/abc"))
            .withFormat(new Csv())
            .withSchema(schema1)
            .createTemporaryTable("s2");
        
        
    
        result.executeInsert("s2");
    }
}
